import requests
import json
import time
import os

from model import Model
from utils import LOG

from zhipuai import ZhipuAI


class ZhipuAIModel(Model):
    def __init__(self, model: str, api_key: str):
        self.model = "glm-4"
        self.api_key = "xxxx"
        self.client = ZhipuAI(api_key=self.api_key)

    def make_request(self, prompt):
        print(type(prompt))
        print(prompt)
        attempts = 0
        while attempts < 3:
            try:
                response = self.client.chat.completions.create(
                    model=self.model,
                    messages=[{"role": "user", "content": prompt}],
                    max_tokens=150,
                )
                # translation = response.choices[0].message.strip()
                translation = response.choices[0].message.content.strip()
                print(type(translation))
                return translation, True
            except Exception as e:
                raise Exception(f"发生了未知错误：{e}")
        return "", False